package com.atlzk.group.order.service.impl;

import com.alipay.api.AlipayApiException;
import com.atlzk.group.order.service.AlipayService;
import com.atlzk.group.order.utils.AlipayEnum;
import com.atlzk.group.order.utils.AlipayTemplate;
import com.atlzk.group.order.vo.PayAsyncVo;
import com.atlzk.group.order.vo.PayVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * @author 泽泽
 * @date 2021/6/20
 */
@Service
@Slf4j
public class AlipayServiceImpl implements AlipayService {

    @Autowired
    private AlipayTemplate alipayTemplate;


    @Override
    public String testPay(PayVo order) throws AlipayApiException {
        return alipayTemplate.pay(order);
    }


    /**
     * 支付宝回调接口
     *
     * */
    @Override
    public void zfbCallBack(PayAsyncVo payAsyncVo) {
        //打印回调信息
        log.info(payAsyncVo.toString());
        //获取支付的状态
        String tradeStatus = payAsyncVo.getTrade_status();

        //判断是否支付成功 支付成功后开始自己的代码逻辑
        if (tradeStatus.equals(AlipayEnum.TRADE_SUCCESS.getStatus())) {
            //修改订单状态

            log.info("支付宝 支付成功");
        } else if (tradeStatus.equals(AlipayEnum.TRADE_CLOSED.getStatus())) {
            log.info("支付宝 未付款交易超时关闭");
        } else if (tradeStatus.equals(AlipayEnum.TRADE_FINISHED.getStatus())) {
            log.info("支付宝 交易结束，不可退款。");
        } else if (tradeStatus.equals(AlipayEnum.WAIT_BUYER_PAY.getStatus())) {
            log.info("支付宝 交易创建，等待买家付款。");
        }
    }

}

